//
//  WTSendGiftOperation.m
//  WTLiveSDKDemo
//
//  Created by ben on 2016/11/23.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTSendGiftOperation.h"
#import "WTAddGiftService.h"
#import "WTLiveSDKHeaders.h"

@interface WTSendGiftOperation ()
@property (nonatomic, getter = isFinished)  BOOL finished;
@property (nonatomic, getter = isExecuting) BOOL executing;
@property (nonatomic,strong) NSArray<WTChatRoomGiftModel*>* models;
@property (nonatomic,copy) NSString*    anchor_id;
@property (nonatomic,copy) WTSendGiftCompletionBlock finishedBlock;
@property (nonatomic,strong) WTAddGiftService*      addGiftService;
@end

@implementation WTSendGiftOperation
@synthesize finished = _finished;
@synthesize executing = _executing;

+ (instancetype)sendGiftOperation:(NSArray<WTChatRoomGiftModel*> *)models anchor_id:(NSString*)anchor_id finishedBlock:(WTSendGiftCompletionBlock)finishedBlock{
    
    WTSendGiftOperation* op = [[WTSendGiftOperation alloc] init];
    op.models = models;
    op.anchor_id = anchor_id;
    op.finishedBlock = finishedBlock;
    return op;
}

- (instancetype)init{
    
    self = [super init];
    if (self) {
        _executing = NO;
        _finished  = NO;
    }
    return self;
}

- (void)start{
    
    if ([self isCancelled]) {
        self.finished = YES;
        return;
    }
    self.executing = YES;
    
    NSInteger total_num = 0;
    NSString* gift_ids = @"";
    NSString* gift_counts = @"";
    for(WTChatRoomGiftModel* model in self.models){
        total_num += model.gift_count * model.gift_price;
        gift_ids = [gift_ids stringByAppendingFormat:@"%zd,",[model.gift_id integerValue]];
        gift_counts = [gift_counts stringByAppendingFormat:@"%zd,",model.gift_count];
    }
    if(gift_ids.length > 0){
        gift_ids = [gift_ids stringByReplacingCharactersInRange:NSMakeRange(gift_ids.length - 1, 1) withString:@""];
    }
    if(gift_counts.length > 0){
        gift_counts = [gift_counts stringByReplacingCharactersInRange:NSMakeRange(gift_counts.length - 1, 1) withString:@""];
    }
    NSLog(@"ids:%@---counts:%@",gift_ids,gift_counts);
    __weak __typeof(self)weakSelf = self;
    [self.addGiftService start:@{@"session_token":WTConfig.session_token,@"anchor_id":@([self.anchor_id longLongValue]),@"gift_num":@(total_num),@"gift_ids":gift_ids,@"gift_counts":gift_counts} completionHandler:^(id responseObject, NSError *error) {
        
        if(!error){
            
            NSDictionary* hashValue = responseObject[@"hash"];
            if(hashValue){
                NSNumber* statusCode = hashValue[@"StatusCode"];
                NSNumber* coins = @([hashValue[@"Msg"] longLongValue]);
                if([statusCode integerValue] == 6){
                    if(weakSelf.finishedBlock){
                        weakSelf.finishedBlock(YES,coins);
                    }
                }else {
                    if(weakSelf.finishedBlock){
                        weakSelf.finishedBlock(NO,nil);
                    }
                }
            }
        }else {
            if(weakSelf.finishedBlock){
                weakSelf.finishedBlock(NO,nil);
            }
        }
        weakSelf.finished = YES;
    }];
    
    while (!self.finished) {
        CFRunLoopRun();
    }
}
#pragma mark -  手动触发 KVO
- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}


#pragma mark getter
- (WTAddGiftService *)addGiftService{
    if(!_addGiftService){
        _addGiftService = [[WTAddGiftService alloc] init];
    }
    return _addGiftService;
}
@end

